﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogView = AMEE.LogViewer;

namespace AMEE.Demo1WPF
{
    public class Logger
    {
        static readonly LogView.MainWindow _instance;
        static int _idxCounter = 0;

        #region Constructor

        static Logger() {
            _instance = new LogView.MainWindow();
            Logger.Write("Start Logging.");
            _instance.Hide();
        }

        ~Logger() {
            _idxCounter = 0;
            _instance.Close();
        }

        #endregion

        public static void Show() {
            _instance.Show();
        }

        public static void Hide() {
            _instance.Hide();
        }

        public static void Fail(String message) {
            string msg = "Error! " + message;
            _instance.LogEntries.Add(
                new LogView.LogEntry() { DateTime = DateTime.Now, Index = Logger._idxCounter++, Message = msg });
        }

        public static void Fail(String message, Exception exc) {
            string msg = "Error! " + typeof(Exception).Name + " in " + exc.Source + ": " + message;
            var mainEntry = new LogView.CollapsibleLogEntry() { DateTime = DateTime.Now, Index = Logger._idxCounter++, Message = msg };
            mainEntry.Contents.Add(new LogView.LogEntry { DateTime = DateTime.Now, Index = Logger._idxCounter++, Message = exc.Message });
            _instance.LogEntries.Add(mainEntry);
        }

        public static void Warn(String message) {
            string msg = "Warning! " + message;
            _instance.LogEntries.Add(
                new LogView.LogEntry() { DateTime = DateTime.Now, Index = Logger._idxCounter++, Message = msg });
        }

        public static void Info(String message) {
            string msg = "Information. " + message;
            _instance.LogEntries.Add(
                new LogView.LogEntry() { DateTime = DateTime.Now, Index = Logger._idxCounter++, Message = msg });
        }

        public static void Write(String message) {
            _instance.LogEntries.Add(
                new LogView.LogEntry() { DateTime = DateTime.Now, Index = Logger._idxCounter++, Message = message });
        }

    }
}
